package com.kuang.controller;

import com.kuang.common.anno.IgnoreToken;
import com.kuang.common.base.BaseController;
import com.kuang.generator.tinyid.ITinyIdGeneratorIdService;
import com.kuang.utils.json.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@RestController
public class TinyIdGeneratorController extends BaseController {

    @Autowired
    private ITinyIdGeneratorIdService tinyIdGeneratorIdService;


    //http://localhost:8089/api/v1/tinyid/generator?bizType=test
    @GetMapping("/tinyid/generator")
    @IgnoreToken
    public Long generatorid(String bizType) {
        return tinyIdGeneratorIdService.genUid(bizType);
    }


    //http://localhost:8089/api/v1/tinyid/generator2?bizType=test
    @GetMapping("/tinyid/generator2")
    @IgnoreToken
    public Map generatorid2(String bizType) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> forEntity = restTemplate.getForEntity(
                "http://localhost:9999/tinyid/id/nextId?bizType="+bizType+"&token=0f673adf80504e2eaa552f5d791b644c", String.class);
        Map string2Obj = JsonUtil.string2Obj(forEntity.getBody(), Map.class);
        return string2Obj;
    }

    //http://localhost:8089/api/v1/tinyid/generators?bizType=test
    @GetMapping("/tinyid/generators")
    @IgnoreToken
    public List<Long> generatorids(String bizType) {
        return tinyIdGeneratorIdService.genUids(bizType, 10);
    }

}
